﻿using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PipeClient;

namespace Demo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            using (NamedPipeClient pipeClient = new NamedPipeClient("mypipe"))
            {
                Console.WriteLine("Attempting to connect to pipe...");
                pipeClient.Connect();

                // Example byte array to send
                byte[] messageBytes = { 0x01, 0x02, 0x03, 0x04 };
                pipeClient.Write(messageBytes, 0, messageBytes.Length);
                Console.WriteLine($"Sent {messageBytes.Length} bytes");

                // Read the response
                byte[] readBuffer = new byte[1024];
                int length = pipeClient.Read(readBuffer, 0, readBuffer.Length);
                Console.WriteLine($"Received {length} bytes");

                // To verify, we print out the received bytes
                Console.WriteLine(BitConverter.ToString(readBuffer, 0, length));

                // Send exit command to close the connection. Here, you need to convert 'exit' to bytes.
                byte[] exitCommand = Encoding.UTF8.GetBytes("exit");
                pipeClient.Write(exitCommand, 0, exitCommand.Length);
                Thread.Sleep(1000);
            }
        }
    }
}
